package core.map
{
	import core.Resource.MapPack;
	import core.Resource.MapPackManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.geom.Point;

	import flash.net.URLRequest;
	
	import loader.core.load.ResParse;
	
	import org.osmf.layout.PaddingLayoutFacet;
	
	import util.PathHelper;


	public class MapTile extends Bitmap
	{

		public var blockName:String = "";
		public var layer:String = "";
		public var sceneName:String = "";
		
		private var _loader:Loader;
		
		//用于处理读取过程中reset无法找到pack的问题；
		private var _resPack:MapPack;


		public function MapTile()
		{
			
		}



		public function setInfo(bName:String,position:Point,layerName:String,sName:String):void

		{

			blockName = bName;
			sceneName = sName;
			layer = layerName;
			
			setPack(MapPackManager.instance.getBlockPack(sName,bName));
			
			this.x = position.x;
			this.y = position.y;
			

		}
		
		private function setPack(pack:MapPack):void
		{
			_resPack = pack;
			if(pack!=null)
			{
				if(pack.state<1)
				{
					pack.addEventListener(Event.COMPLETE,packComplete);
				}
				else
				{
					this.bitmapData = pack.data[blockName];
				}
			}
				
		}
		

		private function packComplete(e:Event):void
		{
			var p:MapPack = e.target as MapPack;
			p.removeEventListener(Event.COMPLETE,packComplete);
			this.bitmapData = p.data[blockName];	
			_resPack = null;
		}
		
		public function reset():void
		{
			if(_loader!=null)
				_loader.unloadAndStop();
			this.x = 0;
			this.y = 0;
			if(this.bitmapData!=null)
				this.bitmapData.dispose();
			this.bitmapData = null;
			blockName = "";
			layer = "";
			sceneName = "";
			if(_resPack!=null && _resPack.hasEventListener(Event.COMPLETE))
				_resPack.removeEventListener(Event.COMPLETE,packComplete);
			_resPack = null;
		}


	}
}